// let BASE_URL = 'https://1033api.myntv.cn/api'
	
let BASE_URL = 'http://1033api.myntv.cn:8003/api/v1'
// let BASE_URL = '/api'
function httpService(url, method, data, header) {
	data = data || {};
	header = header || {
		'Content-Type': 'application/json; charset=UTF-8',
	};
	method = method || "GET";

	// let token = $auth.getUserToken();

	// if (token) {
	// 	header.token = token; // 获取token值
	// }
	let promise = new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			header: header,
			data: data,
			method: method,
			success: res => {
				if (res.data && res.data.success === false) {
					error(res);
				}
				resolve(res);
			},
			fail: (res) => {
				uni.hideLoading();
				error(res);
				reject(res);
			},
			complete: () => {}
		});
	})
	return promise;
}
function httpUpload(url, filePath,  formData, header, success, fail) {
	let name =  'file';
	header = header || {};
	let promise = new Promise((resolve, reject) => {
		uni.uploadFile({
			url: BASE_URL+ url,
			filePath:filePath,
			name: name,
			formData: formData || {},
			success: res => {
				resolve(JSON.parse(res.data));
			},
			fail: (res) => {
				uni.hideLoading();
				let err = JSON.parse(res);
				error(err);
				reject(err);
			},
			complete: () => {}
		});
	})
	return promise;
}
async function getWxUserInfo() {
	uni.getProvider({
		service: 'oauth',
		success: function(res) {
			if (res.provider.indexOf('weixin') != -1) {
				uni.login({
					provider: 'weixin',
					scopes:['auth_user',],
					success: res => {
						uni.setStorageSync('code', res.code)
						uni.request({
							url: BASE_URL+'/default/GetOpenId?code=' + res.code,
							method: 'get',
							success: (d) => {
								let data = JSON.parse(d.data.UserData)
								uni.setStorageSync('openId', data.openid)
								uni.setStorageSync('sessionKey',data.session_key)
							},
						})
					},
					fail: (e) => {
						console.log(e)
						uni.showToast({
							title: '微信登录授权失败',
							icon: 'none'
						});
					}
				});
			} else {
				uni.showToast({
					title: '请先安装微信或升级版本',
					icon: 'none'
				});
			}
		}
	});
}
async function getActionState() {
	httpService('/Mengwa/getMatchState', 'get')
		.then(({
			data
		}) => {
			if (data.Status == 0) {
					uni.setStorageSync('over',false)
			} else {
				uni.showModal({
					title: '提示',
					content: '活动已结束',
					showCancel:false,
					success: function(res) {
						if (res.confirm) {
							uni.setStorageSync('over',true)
						} 
					}
				});
			}
		})
}
export {
	getWxUserInfo,
	getActionState,
	httpService,
	httpUpload
}
